package com.dongdongshop.controller;

import com.dongdongshop.pojo.seller.TbSeller;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {

    @RequestMapping("/")
    public String shopLogin(){
        return "shoplogin";
    }

    @RequestMapping("register")
    public String register(){
        return "register";
    }

    @RequestMapping("login")
    public String login(TbSeller tbSeller, Model model){
        //使用shiro 编写登录逻辑
        // 获取 Subject
        Subject subject = SecurityUtils.getSubject();
        // 将参数封装成 token 对象
        UsernamePasswordToken token = new UsernamePasswordToken(tbSeller.getSellerId(),tbSeller.getPassword());
        //执行登录方法
        try {
            subject.login(token);
            return "admin/index";
        }catch (UnknownAccountException e){//账号（用户）不存在
            model.addAttribute("message","账号不存在,或审核中");
            return "shoplogin";
        }catch (IncorrectCredentialsException e){// 表示密码不正确
            model.addAttribute("message","密码错误");
            return "shoplogin";
        }
    }


}
